package com.zego.wawaji.third.push;

import com.igexin.sdk.PushManager;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushConfig;
import com.tencent.android.tpush.XGPushManager;
import com.zego.wawaji.base.observer.ModuleBase;
import com.zego.wawaji.third.push.server.GeTuiIntentService;
import com.zego.wawaji.third.push.server.GeTuiServer;
import com.zego.wawaji.wawaji_client.App;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

/**
 * 推送工具
 * Created by Su on 2017/12/29.
 */
public class PushMgr implements ModuleBase {

    @Override
    public void init() {
        initPush();
    }

    @Override
    public void release() {
    }

    private void initPush() {
        initXGPush();
        initJPush();
        initGTPush();
    }

    /**
     * XG 推送
     */
    private void initXGPush() {
        XGPushConfig.enableDebug(App.getContext(), false);  // 开启日志, "TPush"过滤

        XGPushManager.registerPush(App.getContext(), new XGIOperateCallback() {
            @Override
            public void onSuccess(Object data, int flag) {
                //token在设备卸载重装的时候有可能会变
//                Log.d("TPush", "注册成功，设备token为：" + data);
            }

            @Override
            public void onFail(Object data, int errCode, String msg) {
//                Log.d("TPush", "注册失败，错误码：" + errCode + ",错误信息：" + msg);
            }
        });
    }

    /**
     * 极光推送
     */
    private void initJPush() {
        JPushInterface.setDebugMode(false);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(App.getContext());            // 初始化 JPush
    }

    /**
     * 以用户id做tag设置给JPush
     */
    public void setJPushTag(long uid) {
        Set<String> set = new HashSet<>();
        set.add(String.valueOf(uid));//名字任意，可多添加几个
        JPushInterface.setAliasAndTags(App.getContext(), String.valueOf(uid), set, null);
    }

    // ========================== 个推 =====================================

    /**
     * clientid is 37cc177d14b0c1d9f35034272e0c5550
     */
    private void initGTPush() {
        PushManager.getInstance().initialize(App.getContext(), GeTuiServer.class);
        PushManager.getInstance().registerPushIntentService(App.getContext(), GeTuiIntentService.class);
    }
}
